import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GodK
 * Date: 2024-08-03
 * Time: 15:59
 */
public class Main {
    private static void scan(File currentFile, String key) {
        if(!currentFile.isDirectory())return;
        File[] files = currentFile.listFiles();
        if(files == null || files.length == 0)return;
        for(File f : files){
            if(f.isFile()){
                doDelete(f,key);
            }else {
                scan(currentFile,key);
            }
        }
    }

    private static void doDelete(File f, String key) {
        if(!f.getName().contains(key)){
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println(f.getAbsolutePath() + " 是否确认要删除 Y/n");
        String choice = scanner.next();
        if (choice.equals("Y") || choice.equals("y")) {
            f.delete();
        }
    }

    public static void main(String[] args) {
        System.out.println("请输入要搜索的路径: ");
        Scanner scanner = new Scanner(System.in);
        String rootPath = scanner.next();
        File rootFile = new File(rootPath);
        if(!rootFile.isDirectory()) {
            System.out.println("输入的路径不存在");
            return;
        }
        System.out.println("请输入要删除的文件名字的关键字");
        String key = scanner.next();
        scan(rootFile,key);
    }
}
